package edu.dlnu.newbootdemo.controller;

import edu.dlnu.newbootdemo.pojo.Student;
import edu.dlnu.newbootdemo.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.rmi.StubNotFoundException;
import java.util.List;

@RestController
// 解决跨域
@CrossOrigin
public class StudentController {
    @Autowired
    private IStudentService service;

    // restful风格
    @GetMapping("/stus/{curr}")
    public List<Student> getStudentsByPage(@PathVariable("curr") int currPage) {
        return service.getStudents(currPage);
    }

    // 根据学号查询
    @GetMapping("/query/{sno}")
    public Student queryById(@PathVariable("sno") int sno) {
        return service.queryStudentByNo(sno);
    }

    // 增加学生 Post请求
    @PostMapping("/addStudent")
    // requestBody 解析json格式参数
    public String add(@RequestBody Student student){
        return service.addStudent(student) ? "增加成功" : "增加失败";
    }

    // 修改
    @PutMapping("/updateStudent")
    public String update(@RequestBody Student student) {
        return service.updateStudent(student) ? "修改成功" : "修改失败";
    }

    // 删除
    @DeleteMapping("/delStudent/{sno}")
    public String del(@PathVariable("sno") int sno) {
        return service.delStudent(sno) ? "删除成功" : "删除失败";
    }
}
